package com.hailian.fintech.hlautoserver.controller;


import com.hailian.fintech.hlautoserver.entity.User;
import com.hailian.fintech.hlautoserver.service.IUserService;
import com.hailian.fintech.hlcomm_interfaceserver.result.ResultInfo;
import com.hailian.fintech.hlcomm_interfaceserver.util.GetMessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.security.Principal;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wk
 * @since 2020-05-20
 */
@RestController
@RequestMapping("/user")
public class UserController {
   @Autowired
    private IUserService iUserService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 测试方法
     * @return
     */
   @RequestMapping("test")
    public  Object test(){
        User byId = iUserService.getById(1);
        return  byId;
    }

    /**
     * 资源中心回调
     * @param member
     * @return
     */
    @GetMapping("/info")
    public Principal user(Principal member) {
        return member;
    }


    @Autowired
    ConsumerTokenServices consumerTokenServices;

    /**
     * 退出登录
     * @param access_token
     * @return
     */
    @RequestMapping("loginOut")
    public Object loginOut(String access_token){
        if(consumerTokenServices.revokeToken(access_token)){
            return new ResultInfo().success("注销成功");
        }else {
            return new ResultInfo().err("注销失败");
        }
    }

    /**
     * 获取手机验证码
     * @param access_token
     * @return
     */
    @RequestMapping("getMessageCode")
    public Object getMessageCode(String access_token,String phone){
        String code = GetMessageCode.getCode(phone);
        return new ResultInfo().success(code);
    }
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("showinfo")
    public Object showinfo(@RequestParam("username") String username){
        User user = iUserService.showinfo(username);
        return new ResultInfo().success(user);
    }

    /**
     * 初始化支付密码
     * @param
     * @return
     */
    @RequestMapping("initDeal")
    public Object initPassword(@RequestBody User user){
        if(user.getNew_password() != null && user.getNew_password() != ""){
            user.setPasswprd(passwordEncoder.encode(user.getNew_password()));
        }
        boolean flag = iUserService.saveOrUpdate(user);
        return new ResultInfo().success(flag);
    }
    /**
     * 余额改变
     * @param
     * @return
     */
    @RequestMapping("moneyChange")
    public Object moneyChange(@RequestParam("id") Integer id, @RequestParam("stauts")String stauts, @RequestParam("money")BigDecimal money){
        User user = iUserService.getById(id);
        if(stauts.equals("-")){
            user.setMoney(new BigDecimal(user.getMoney().intValue()-money.intValue()));
        }else {
            user.setMoney(new BigDecimal(user.getMoney().intValue()+money.intValue()));
        }
        boolean flag = iUserService.saveOrUpdate(user);
        return new ResultInfo().success(flag);
    }



}

